
public class Octree {
	public Octree[] child;
	
	int chunkId;
	byte type;
	int offsetX, offsetY, offsetZ;
	
	//visibility
	public boolean north, south, east, west, top, bottom;
	
	public Octree parent;
	
	public Octree(Octree parent)
	{
		this.parent = parent;
		child = new Octree[8];
	}
	
	public Octree(byte type, int offsetX, int offsetZ)
	{
		this.type = type;
		this.offsetX = offsetX;
		this.offsetZ = offsetZ;
	}
	
	public int getDepth(Octree oct)
	{
		int result = 0;
		if(oct.parent != null)
		{
			result += getDepth(oct.parent) + 1;
			return result;
		}
		return 0;
	}
	
	//recursively step through octree to determine the position in space each octant takes
	public void generateOffsets()
	{
		if(child[0] != null)
		{
			child[0].offsetX = offsetX;
			child[0].offsetY = offsetY;
			child[0].offsetZ = offsetZ;
			child[0].generateOffsets();
		}
		if(child[1] != null)
		{
			child[1].offsetX = offsetX + getOffset(child[1]);
			child[1].offsetY = offsetY;
			child[1].offsetZ = offsetZ;
			child[1].generateOffsets();
		}
		if(child[2] != null)
		{
			child[2].offsetX = offsetX + getOffset(child[2]);
			child[2].offsetY = offsetY;
			child[2].offsetZ = offsetZ + getOffset(child[2]);
			child[2].generateOffsets();
		}
		if(child[3] != null)
		{
			child[3].offsetX = offsetX;
			child[3].offsetY = offsetY;
			child[3].offsetZ = offsetZ  + getOffset(child[3]);
			child[3].generateOffsets();
		}
		if(child[4] != null)
		{
			child[4].offsetX = offsetX;
			child[4].offsetY = offsetY + getOffset(child[4]);
			child[4].offsetZ = offsetZ;
			child[4].generateOffsets();
		}
		if(child[5] != null)
		{
			child[5].offsetX = offsetX + getOffset(child[5]);
			child[5].offsetY = offsetY + getOffset(child[5]);
			child[5].offsetZ = offsetZ;
			child[5].generateOffsets();
		}
		if(child[6] != null)
		{
			child[6].offsetX = offsetX + getOffset(child[6]);
			child[6].offsetY = offsetY + getOffset(child[6]);
			child[6].offsetZ = offsetZ + getOffset(child[6]);
			child[6].generateOffsets();
		}
		if(child[7] != null)
		{
			child[7].offsetX = offsetX;
			child[7].offsetY = offsetY + getOffset(child[7]);
			child[7].offsetZ = offsetZ  + getOffset(child[7]);
			child[7].generateOffsets();
		}
	}
	
	
	public int getOffset(Octree o)
	{
		switch(getDepth(o))
		{
			case 0: return 16;
			case 1: return 8;
			case 2: return 4;
			case 3: return 2;
			case 4: return 1;
			default: System.out.println("Error getting Octree offset: out of bounds."); 
					 return 0;
		}
	}
	
	public void setVisible(boolean b)
	{
		if(b)
		{
			north = true;
			south = true;
			east = true;
			west = true;
			top = true;
			bottom = true;
		}
		else
		{
			north = false;
			south = false;
			east = false;
			west = false;
			top = false;
			bottom = false;
		}
	}
}